package com.teamb6.shop.service.impl;

import com.teamb6.shop.domain.Collection;
import com.teamb6.shop.domain.CollectionKey;
import com.teamb6.shop.domain.Goods;
import com.teamb6.shop.mapper.CollectionMapper;
import com.teamb6.shop.mapper.GoodsMapper;
import com.teamb6.shop.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CollectionServiceImpl implements CollectionService {
    @Autowired
    CollectionMapper collectionMapper;
    @Autowired
    GoodsMapper goodsMapper;
    //添加、取消收藏
    @Override
    public int changeCollection(Collection collection) {
        CollectionKey collectionKey = new CollectionKey();
        collectionKey.setUserid(collection.getUserid());
        collectionKey.setGoodsid(collection.getGoodsid());
        Collection flagCollection = collectionMapper.selectByPrimaryKey(collectionKey);
        if (flagCollection == null) {
            if (collectionMapper.insertSelective(collection)==0) return 0;
            return 1; //1表示添加收藏
        }else {
            if (collectionMapper.deleteByPrimaryKey(collectionKey)==0) return 0;
            return 2; //2表示取消收藏
        }
    }
    //查询所有收藏
    @Override
    public List<Goods> getAllCollection(Integer userId) {
        return goodsMapper.selectAllCollection(userId);
    }
    //删除收藏商品
    @Override
    public int deleteCollection(Integer userId,Integer goodsId) {
        CollectionKey collectionKey = new CollectionKey();
        collectionKey.setUserid(userId);
        collectionKey.setGoodsid(goodsId);
        return collectionMapper.deleteByPrimaryKey(collectionKey);
    }
    //根据主键查询
    @Override
    public Collection getByCollectionKey(Integer userId, Integer goodsId) {
        CollectionKey collectionKey=new CollectionKey();
        collectionKey.setUserid(userId);
        collectionKey.setGoodsid(goodsId);
        return collectionMapper.selectByPrimaryKey(collectionKey);
    }

}
